/*
 * To change this license header, choose License Headers in Project Properties. To change this
 * template file, choose Tools | Templates and open the template in the editor.
 */
package chalmers.game.subterranean.ui.view;

import chalmers.game.subterranean.event.events.world.shopevents.AddShopItem;
import chalmers.game.subterranean.event.events.world.shopevents.CloseShopEvent;
import chalmers.game.subterranean.event.events.world.shopevents.OpenShopEvent;
import chalmers.game.subterranean.util.options.Backgrounds;
import chalmers.game.subterranean.util.options.WorldValues;
import com.google.common.eventbus.Subscribe;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;

/**
 * 
 * @author Albin
 */
public class ShopView {

  public GridPane layout;
  FlowPane upgradePane;
  FlowPane itemPane;
  Label upgradeLbl;
  Label itemLbl;

  public ShopView() {
    layout = new GridPane();
    upgradeLbl = new Label("Upgrades");
    itemLbl = new Label("Items");
    upgradePane = new FlowPane();
    itemPane = new FlowPane();

    upgradePane.setBackground(Backgrounds.SHOPFOREGROUND_BG);
    upgradePane.setPrefSize(296, 398);
    upgradePane.setPadding(new Insets(2));
    upgradeLbl.setPrefHeight(48);
    upgradeLbl.setFont(new Font(30));

    itemPane.setBackground(Backgrounds.SHOPFOREGROUND_BG);
    itemPane.setPrefSize(296, 398);
    itemPane.setPadding(new Insets(2));
    itemLbl.setPrefHeight(48);
    itemLbl.setFont(new Font(30));

    layout.setPrefSize(300, 400);
    layout.setPadding(new Insets(2));
    layout.setBackground(Backgrounds.SHOPBACKGROUND_BG);
    layout.add(upgradeLbl, 0, 0);
    layout.add(upgradePane, 0, 1, 1, 4);
    layout.add(itemLbl, 0, 5);
    layout.add(itemPane, 0, 6, 1, 4);

    layout.relocate(WorldValues.BLOCK_SIZE * 8, WorldValues.BLOCK_SIZE * 4);
    // layout.setVisible(false);
  }

  @Subscribe
  private Pane addPane(AddShopItem item) {
    Pane pane = new Pane();
    pane.setPrefSize(90, 70);
    pane.setBackground(Backgrounds.SHOPBACKGROUND_BG);

    Label itemLbl = new Label(item.name);
    Label levelLbl = new Label("Level = " + item.level);
    Label priceLbl = new Label("Cost: " + item.cost);

    itemLbl.relocate(5, 5);
    itemLbl.setFont(new Font(20));
    itemLbl.setOnMouseClicked(new EventHandler<MouseEvent>() {

      @Override
      public void handle(MouseEvent event) {
        // post(item);
      }
    });

    levelLbl.relocate(70, 7);
    priceLbl.relocate(70, 60);

    pane.getChildren().addAll(itemLbl, levelLbl, priceLbl);

    return pane;
  }

  public void clearShop() {
    upgradePane.getChildren().clear();
    itemPane.getChildren().clear();
  }

  // public GridPane prepareShop(ArrayList<> upgrades, ArrayList<Item> items) {
  // for (Item item : upgrades) {
  // upgradePane.getChildren().add(createItemPane(item));
  // }
  // for (Item item : items) {
  // itemPane.getChildren().add(createItemPane(item));
  // }
  // return layout;
  // }
  @Subscribe
  public void openShopHandler(OpenShopEvent evt) {
    layout.setVisible(true);
  }

  @Subscribe
  public void openShopHandler(CloseShopEvent evt) {
    layout.setVisible(false);
  }

}
